<?php

class Application_Model_Izdelki
{
    
    protected $_idprodukt;
    protected $_ime;
    protected $_cena;
    protected $_zaloga;
    protected $_thumbnail;
    protected $_base_image;
    protected $_aktiviran;

    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid product property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid product property');
        }
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
    
    public function setIdProdukt($produkt)
    {
        $this->_idprodukt = $produkt;
        return $this;
    }

    public function getIdProdukt()
    {
        return $this->_idprodukt;
    }

    public function setIme($text)
    {
        $this->_ime = (string) $text;
        return $this;
    }

    public function getIme()
    {
        return $this->_ime;
    }

    public function setCena($cena)
    {
        $this->_cena = $cena;
        return $this;
    }

    public function setAktiviran($akt)
    {
        $this->_aktiviran = $akt;
        return $this;
    }

    public function getCena()
    {
        return $this->_cena;
    }

    public function setZaloga($zaloga)
    {
        $this->_zaloga = $zaloga;
        return $this;
    }

    public function getZaloga()
    {
        return $this->_zaloga;
    }
    
    public function setThumbNAil($thumbnail)
    {
        $this->_thumbnail = $thumbnail;
        return $this;
    }

    public function getThumbNail()
    {
        return $this->_thumbnail;
    }
    
    public function setBaseImage($baseimage)
    {
        $this->_base_image = $baseimage;
        return $this;
    }

    public function getBaseImage()
    {
        return $this->_base_image;
    }

    public function getAktiviran()
    {
        return $this->_aktiviran;
    }

    public function getEditUrl($baseUrl)
    {
        return $baseUrl."/prodaja/edit-product/?id=".base64_encode($this->getIdProdukt());
    }

    public function getDeleteUrl($baseUrl)
    {
        return $baseUrl."/prodaja/delete-product/?id=".base64_encode($this->getIdProdukt());
    }

    public function getActivateUrl($baseUrl)
    {
        return $baseUrl."/prodaja/activate-product/?id=".base64_encode($this->getIdProdukt());
    }
    public function getDeactivateUrl($baseUrl)
    {
        return $baseUrl."/prodaja/deactivate-product/?id=".base64_encode($this->getIdProdukt());
    }
    
    public function getDetailsUrl($baseUrl)
    {
        return $baseUrl."/seznam/podrobnosti/?id=".base64_encode($this->getIdProdukt());
    }
    
    public function getDodajKosaricaUrl($baseUrl)
    {
        return $baseUrl."/kosarica/dodaj/?id=".base64_encode($this->getIdProdukt());
    }
    
    public function jeNaVoljo()
    {
        
        if ($this->getZaloga() > 0 && $this->getAktiviran() == 1) {
            return true;
        } else {
            return false;
        }
        
    }

    public function getPovprecnaOcena() {
        $ocenaMapper = new Application_Model_OcenaMapper();
        $where = $ocenaMapper->getDbTable()->getAdapter()->quoteInto('produkt_idprodukt = ?', $this->getIdProdukt());

        $ocene = $ocenaMapper->fetchAll($where);
        $total = 0;
        foreach ($ocene as $ocena) {
            $total += $ocena->ocena;
        }

        return $total / count($ocene);
    }
    
    

}